package net
{
	import flash.utils.ByteArray;

	public class PackageOut extends ByteArray
	{
		public static const HEADER:int = 0x71ab;
		
		private var _checksum:int;
		
		private var _code:int;
		
		public function get code():int
		{
			return _code;
		}
		
		public function PackageOut(code:int,toId:int = 0,extend1:int = 0,extend2:int = 0)
		{
			super();
			writeShort(HEADER);	//标志符
			writeInt(0x00);	//长度
//			writeShort(0x00); 	//check sum
			writeInt(code);	//协议号
//			writeInt(toId);
			writeInt(extend1);
			writeInt(extend2);
			_code = code;
			_checksum = 0;
		}
		
		public function pack():void
		{
			_checksum = calculateCheckSum();
			var temp:ByteArray = new ByteArray();
			temp.writeInt(length);
			this[2] = temp[0];
			this[3] = temp[1];
			this[4] = temp[2];
			this[5] = temp[3];
		}
		
		public function calculateCheckSum():int
		{
            var val1:int = 0x77;
            var i:int = 6;
            while (i < length)
            {
                val1 += this[i++];
            }

            return (val1) & 0x7F7F;
		}
				
		public function writeDate(date:Date):void
		{
			writeShort(date.getFullYear());
			writeByte(date.month+1);
			writeByte(date.date);
			writeByte(date.hours);
			writeByte(date.minutes);
			writeByte(date.seconds);
		}
	}
}